﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using EnvDTE;
using EnvDTE80;

namespace Crude.VisualStudioAddin.BuildEngine
{
    public class Logger : ILogger
    {
         DTE2 m_applicationObject;
         public string m_taskName;

         OutputWindowPane m_outputWindowPane;

         public Logger(DTE2 applicationObject, string taskName)
         {
             m_applicationObject = applicationObject;
             m_taskName = taskName;
         }

        public void Initialize(IEventSource eventSource)
        {
            eventSource.BuildStarted += new BuildStartedEventHandler(eventSource_BuildStarted);
            eventSource.BuildFinished += new BuildFinishedEventHandler(eventSource_BuildFinished);
            eventSource.MessageRaised += new BuildMessageEventHandler(eventSource_MessageRaised);
            eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised);
            eventSource.ErrorRaised += new BuildErrorEventHandler(eventSource_ErrorRaised);
            eventSource.StatusEventRaised += new BuildStatusEventHandler(eventSource_StatusEventRaised);            
           
            try
            {
                m_outputWindowPane = m_applicationObject.ToolWindows.OutputWindow.OutputWindowPanes.Item("Crude");
            }
            catch
            {
                m_outputWindowPane = m_applicationObject.ToolWindows.OutputWindow.OutputWindowPanes.Add("Crude");
            }

            m_outputWindowPane.Activate();
        }

        void eventSource_StatusEventRaised(object sender, BuildStatusEventArgs e)
        {
            
        }

        void eventSource_ErrorRaised(object sender, BuildErrorEventArgs e)
        {
            m_outputWindowPane.OutputString(e.Message);
            m_outputWindowPane.OutputString("\r\n");
        }

        void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
        {
            m_outputWindowPane.OutputString(e.Message);
            m_outputWindowPane.OutputString("\r\n");
        }

        void eventSource_MessageRaised(object sender, BuildMessageEventArgs e)
        {
            if (e.Importance != MessageImportance.Low)
            {
                m_outputWindowPane.OutputString(e.Message);
                m_outputWindowPane.OutputString("\r\n");
            }
        }

        void eventSource_BuildFinished(object sender, BuildFinishedEventArgs e)
        {
            m_outputWindowPane.OutputString(e.Message);
            m_outputWindowPane.OutputString("\r\n");
            m_applicationObject.StatusBar.Text = e.Message;
        }

        void eventSource_BuildStarted(object sender, BuildStartedEventArgs e)
        {
            m_outputWindowPane.Clear();
            m_outputWindowPane.OutputString(e.Message);
            m_outputWindowPane.OutputString("\r\n");
            m_applicationObject.StatusBar.Text = e.Message;
        }

        public string Parameters
        {
            get;
            set;
        }

        public void Shutdown()
        {
            
        }

        public LoggerVerbosity Verbosity
        {
            get;
            set;
        }
    }
}
